home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / ENTRPRIS / APE / AEWORKER / WORKER.CLS < prev   
Encoding:
Visual Basic class definition  |  1996-12-04  |  16.3 KB  |  367 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Worker"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Attribute VB_Description = "Fulfills APE Service Requests, delegated by the AEQueueMgr."
  11. Option Explicit
  12. '-------------------------------------------------------------------------
  13. 'The Class is the only public class in this project.  See notes in
  14. 'modWorker for purpose.
  15. '-------------------------------------------------------------------------
  16.  
  17. '***********************
  18. 'Public Properties
  19. '***********************
  20.  
  21. Public Property Set QueueMgrRef(ByVal oQueueMgr As APEInterfaces.QueueDelegator)
  22. Attribute QueueMgrRef.VB_Description = "Sets the QueueDelegator object that the Worker uses to receive Service Requests and to return Service Request results to the AEQueueMgr."
  23.     '-------------------------------------------------------------------------
  24.     'Purpose:   Called by the the QueueMgr to pass a reference of itself to
  25.     '           the Worker.
  26.     'In:        [oQueueMgr]
  27.     '               A valid reference to a QueueMgr class object
  28.     'Effects:   [goQueueDelegator]
  29.     '               Sets the global object variable equal to the passed reference
  30.     '-------------------------------------------------------------------------
  31.     Set goQueueDelegator = oQueueMgr
  32.     
  33. End Property
  34.  
  35. Public Property Let Log(ByVal bLog As Boolean)
  36. Attribute Log.VB_Description = "Determines if the Worker logs its events and errors to the AELogger.Logger object."
  37.     '-------------------------------------------------------------------------
  38.     'Purpose:   If property is true, the Worker worker generates log records
  39.     '           for events and errors and passes the records to the logger.
  40.     'Effects:   [goLogger]
  41.     '               if blog is true sets this equal to new logger object
  42.     '               Otherwise, the it is destroyed
  43.     '-------------------------------------------------------------------------
  44.     If Not gbLog = bLog Then
  45.         gbLog = bLog
  46.         If bLog = True Then
  47.             Set goLogger = New AELogger.Logger
  48.         Else
  49.             Set goLogger = Nothing
  50.         End If
  51.      End If
  52. End Property
  53.  
  54. Public Property Get Log() As Boolean
  55.     Log = gbLog
  56. End Property
  57.  
  58. Public Property Let ID(ByVal lID As Long)
  59. Attribute ID.VB_Description = "Returns or Sets the ID used by the AEQueueMgr or AEPoolMgr to manage this Worker."
  60.     '-------------------------------------------------------------------------
  61.     'Purpose:   Called by QueueMgr to give the Worker a unique ID.  This ID
  62.     '           can only be set once.  The Worker must have this ID to poll
  63.     '           the QueueMgr.
  64.     'Effects:
  65.     '   [glWorkerID]
  66.     '           Is set equal to the passed long, if it has not already happened
  67.     '-------------------------------------------------------------------------
  68.     Static stbAlreadySet As Boolean
  69.     If Not stbAlreadySet Then
  70.         glWorkerID = lID
  71.         stbAlreadySet = True
  72.     End If
  73. End Property
  74.  
  75. Public Property Get ID() As Long
  76.     ID = glWorkerID
  77. End Property
  78.  
  79. Public Property Let PersistentServices(ByVal bPersistent As Boolean)
  80. Attribute PersistentServices.VB_Description = "Determines whether the Worker retains references to service objects that it instantiates or if it releases their references after each use."
  81.     '-------------------------------------------------------------------------
  82.     'Purpose:   If true, the worker keeps reference to
  83.     '           every Service object it has used, else
  84.     '           worker releases Service object after
  85.     '           each use.
  86.     'Effects:
  87.     '   [gcServices]
  88.     '           If property is being changed to false
  89.     '           then set collection of Service Object
  90.     '           references to nothing, if it is change
  91.     '           to true set equal to new collection
  92.     '-------------------------------------------------------------------------
  93.     If gbPersistent <> bPersistent Then
  94.         gbPersistent = bPersistent
  95.         If Not bPersistent Then
  96.             Set gcServices = Nothing
  97.         Else
  98.             Set gcServices = New Collection
  99.         End If
  100.     End If
  101. End Property
  102.  
  103. Public Property Get PersistentServices() As Boolean
  104.     PersistentServices = gbPersistent
  105. End Property
  106.  
  107. Public Property Let EarlyBindServices(ByVal bEarlyBind As Boolean)
  108. Attribute EarlyBindServices.VB_Description = "Specifies whether service objects should be instantiated as APEInterfaces.Service class objects or as Object class objects.  If true, all service objects must implement the APEInterfaces.Service interface."
  109.     '-------------------------------------------------------------------------
  110.     'Purpose:   If true, the worker uses code that utilizes
  111.     '           early binding.  This option is only
  112.     '           available for Service objects classes that
  113.     '           were developed with the worker class,
  114.     '           because early binding is only available
  115.     '           if class names are hard coded.
  116.     'Effects:
  117.     '   [gbEarlyBind] is made equal to passed Boolean
  118.     '-------------------------------------------------------------------------
  119.     gbEarlyBind = bEarlyBind
  120. End Property
  121.  
  122. Public Property Get EarlyBindServices() As Boolean
  123.     EarlyBindServices = gbEarlyBind
  124. End Property
  125.  
  126. '************************
  127. 'Public Methods
  128. '************************
  129. Public Sub SetProperties(Optional ByVal bLog As Variant, _
  130.         Optional ByVal bEarlyBindServices As Variant, _
  131.         Optional ByVal bPersistentServices As Variant, _
  132.         Optional ByVal lID As Variant)
  133. Attribute SetProperties.VB_Description = "Sets Worker properties in one method call."
  134.     '-------------------------------------------------------------------------
  135.     'Purpose:   Called by QueueMgr or Client to set properties with one method
  136.     '           call
  137.     'Effects:
  138.     '       [Properties]
  139.     '           Log, EarlyBindServices, ID, PersistentServices, UseQueueMgr
  140.     '-------------------------------------------------------------------------
  141.         With Me
  142.             .Log = bLog
  143.             If Not IsMissing(bEarlyBindServices) Then gbEarlyBind = bEarlyBindServices
  144.             If Not IsMissing(lID) Then .ID = lID
  145.             If Not IsMissing(bPersistentServices) Then .PersistentServices = bPersistentServices
  146.         End With
  147. End Sub
  148.         
  149. Public Function DoService(ByVal lServiceID As Long, ByVal sCommand As String, Optional ByVal vData As Variant) As Variant
  150. Attribute DoService.VB_Description = "Receives a Service Request, loads the object needed to fulfill the Service Request, and returns the results."
  151.     '-------------------------------------------------------------------------
  152.     'Purpose:   This method allows a client to accomplish the same tasks
  153.     '           accomplished by the return of a task request when the worker calls
  154.     '           the QueueMgr in PollQueue or CheckService, but uses a synchronous
  155.     '           process instead of asynchronous.
  156.     '           This method is intended to be called directly by a client rather than
  157.     '           the QueueMgr.  It is provided for using a Pool Manager system or
  158.     '           Direct instanciation system
  159.     '
  160.     '           This loads a service object, and may or may not keep it in a collection
  161.     '           for future use.  The service object is called to accomplish the
  162.     '           requested task and then the return results of the service object
  163.     '           are returned directly to the calling client
  164.     'IN:
  165.     '   [lServiceID]
  166.     '           An ID for the requested Service request.  It is only useful for
  167.     '           tracing log records.
  168.     '   [sCommand]
  169.     '           ProgID and Method or Task name in the formate of "Library.Class.Method"
  170.     '           The "Library.Class" is used to load the needed Service object.  The
  171.     '           "Method" string is passed to the Execute method of the service object
  172.     '   [vData]
  173.     '           (Optional) Data passed by the client that gets passed to the Service
  174.     '           object.  This data is not manipulated by the Worker at all.
  175.     'Return:
  176.     '           Variant: this value is obtained by calling the service object.  It is
  177.     '           the results of the task accomplished by the service object.  It is not
  178.     '           manipulated by the Worker at all.
  179.     'Effects:
  180.     '   [goEarlyBoundService]
  181.     '           Set equal to AEService.Service class object if passed ProgID
  182.     '           equals "AEService.Service"
  183.     '   [gsLastCommandUsed]
  184.     '           Set equal to sCommand parameter
  185.     '   [gsLastLibClassUsed]
  186.     '           Set equal to the Library.Class in the sCommand Parameter
  187.     '   [gsLastMethodUsed]
  188.     '           Set equal to the method portion of the passed sCommand parameter
  189.     '   [goLastServiceUsed]
  190.     '           Set equal to the object created using the passed ProgID
  191.     '-------------------------------------------------------------------------
  192.     Dim vReturn As Variant
  193.     Dim bDataPresent As Boolean
  194.     If Not IsMissing(vData) Then bDataPresent = True Else bDataPresent = False
  195.     If Not gsLastCommandUsed = sCommand Then
  196.         gsLastCommandUsed = sCommand
  197.         'Get the library.class from sCommand
  198.         'sCommand is in the format "library.class.method"
  199.         GetLibClassMethod gsLastLibClassUsed, gsLastMethodUsed, sCommand
  200.         'Get the Service object
  201.         Set goLastServiceUsed = GetServiceObject(gsLastLibClassUsed)
  202.         If gbEarlyBind Then
  203.             Set goEarlyBoundService = goLastServiceUsed
  204.         End If
  205.     End If
  206.     
  207.     'Call the execute method of the class object
  208.     'passing the method string and the send data as variant,
  209.     'and the return data as variant by reference
  210.     If gbEarlyBind Then
  211.     'Use the Earlybound object reference
  212.         If bDataPresent Then
  213.             goEarlyBoundService.Execute gsLastMethodUsed, vData, vReturn
  214.         Else
  215.             goEarlyBoundService.Execute gsLastMethodUsed
  216.         End If
  217.     Else
  218.         If bDataPresent Then
  219.             goLastServiceUsed.Execute gsLastMethodUsed, vData, vReturn
  220.         Else
  221.             goLastServiceUsed.Execute gsLastMethodUsed
  222.         End If
  223.     End If
  224.     
  225.     'Return the data
  226.     Select Case VarType(vReturn)
  227.         Case vbNull, vbEmpty
  228.             DoService = Null
  229.         Case vbObject, vbError, vbDataObject
  230.             Set DoService = vReturn
  231.         Case Else
  232.             DoService = vReturn
  233.     End Select
  234. End Function
  235.  
  236.  
  237. Public Sub LoadServiceObject(ByVal ServiceLibClass As String)
  238. Attribute LoadServiceObject.VB_Description = "Loads an object whose ProgID matches ServiceLibClass, if PersistentServices is true."
  239.     '-------------------------------------------------------------------------
  240.     'Purpose:   Method is provided to instruct worker to Load an Service
  241.     '           object without calling the execute method of the Service
  242.     '           object.
  243.     'IN:
  244.     '   [ServiceLibClass]
  245.     '           String that contains a ProgID. ServiceLibClass may be in the
  246.     '           format "library.class.method" or "library.class"
  247.     'Effects:
  248.     '   [goEarlyBoundService]
  249.     '           Set equal to AEService.Service class object if passed ProgID
  250.     '           equals "AEService.Service"
  251.     '   [gsLastCommandUsed]
  252.     '           Set equal to ServiceLibClass parameter
  253.     '   [gsLastLibClassUsed]
  254.     '           Set equal to the Library.Class in the ServiceLibClass Parameter
  255.     '   [gsLastMethodUsed]
  256.     '           Set equal to the method portion of the passed ServiceLibClass parameter
  257.     '   [goLastServiceUsed]
  258.     '           Set equal to the object created using the passed ProgID
  259.     '-------------------------------------------------------------------------
  260.     'Method is provided to instruct worker to Load an Service
  261.     'object without calling the execute method of the Service
  262.     'object
  263.     Dim oService As Object
  264.     Dim sMethod As String
  265.     Dim iPos As Integer
  266.     gsLastCommandUsed = ServiceLibClass
  267.     'Get the library.class from ServiceLibClass
  268.     'ServiceLibClass may be in the format "library.class.method"
  269.     'or "library.class"
  270.     iPos = InStr(ServiceLibClass, gsCOMMAND_DELIMITER)
  271.     If iPos = 0 Then Err.Raise giINVALID_COMMAND_PARAMETER, , LoadResString(giINVALID_COMMAND_PARAMETER)
  272.     iPos = InStr((iPos + 1), ServiceLibClass, gsCOMMAND_DELIMITER)
  273.     If iPos = 0 Then
  274.         gsLastLibClassUsed = ServiceLibClass
  275.     Else
  276.         gsLastLibClassUsed = Left$(ServiceLibClass, (iPos - 1))
  277.         gsLastMethodUsed = Right$(ServiceLibClass, Len(ServiceLibClass) - iPos)
  278.     End If
  279.     'Get the Service object
  280.     Set goLastServiceUsed = GetServiceObject(gsLastLibClassUsed)
  281.     If gbEarlyBind Then
  282.         Set goEarlyBoundService = goLastServiceUsed
  283.     End If
  284. End Sub
  285.  
  286. Public Sub ShutDown()
  287. Attribute ShutDown.VB_Description = "Causes Worker to stop processing a Service Requests and destroy its QueueDelegator object."
  288.     '-------------------------------------------------------------------------
  289.     'Purpose:   Shut down the Worker.  Timer is Killed.  Reference to Queue
  290.     '           Manager is destroyed.
  291.     'Effects:
  292.     '   [gbShutDown]
  293.     '           Is set to false.
  294.     '-------------------------------------------------------------------------
  295.     gbShutDown = True
  296.     SetEnabled False
  297. End Sub
  298.  
  299. Public Function GetLogger() As AELogger.Logger
  300. Attribute GetLogger.VB_Description = "Returns the AELogger.Logger object instantiated by this Worker."
  301.     '-------------------------------------------------------------------------
  302.     'Purpose:   Get the logger object local to this worker
  303.     'Return:    A valid AELogger.Logger object on the same machine as
  304.     '           this Worker class object
  305.     '-------------------------------------------------------------------------
  306.     Set GetLogger = goLogger
  307. End Function
  308.  
  309. Public Sub StartPollingQueue()
  310. Attribute StartPollingQueue.VB_Description = "Causes the Worker to start polling the GetServiceRequest method of the QueueDelegator object,  if the QueueDelegator object is set."
  311.     '-------------------------------------------------------------------------
  312.     'Purpose:   Provided for the QueueMgr to cause the Worker to start
  313.     '           polling the QueueMgr
  314.     'Effects:
  315.     '           Starts timer so that Worker begins polling Queue
  316.     '-------------------------------------------------------------------------
  317.     'Start timer to pole queuemgr if not processing a service already
  318.     If (Not gbNewService) And (Not goQueueDelegator Is Nothing) Then SetEnabled True
  319. End Sub
  320.  
  321. '*********************
  322. 'Private procedures
  323. '*********************
  324.  
  325. Private Sub Class_Initialize()
  326.     On Error GoTo Class_InitializeError
  327.     '-------------------------------------------------------------------------
  328.     'Purpose:   Puts Worker in its initial state setting many globals
  329.     '           to their defaults, if mlInstances = 1 after adding one to it.
  330.     '-------------------------------------------------------------------------
  331.     App.OleServerBusyRaiseError = True
  332.     App.OleServerBusyTimeout = 10000
  333.     'Set default property values
  334.     'Create Logger class object if gbLog is true
  335.     If gbLog Then Set goLogger = New AELogger.Logger
  336.     gbLog = gbLOG_DEFAULT
  337.     gbPersistent = gbPERSISTENCE_DEFAULT
  338.     gbEarlyBind = gbEARLY_BIND_DEFAULT
  339.     'Create Logger class object if gbLog is true
  340.     If gbLog Then Set goLogger = New AELogger.Logger
  341.     'Create cServices collection if gbPersistent
  342.     If gbPersistent Then Set gcServices = New Collection
  343.     SetInterval giTIMER_INTERVAL
  344.     Exit Sub
  345. Class_InitializeError:
  346.     LogError Err, 0
  347.     Resume Next
  348. End Sub
  349.  
  350. Private Sub Class_Terminate()
  351.     '-------------------------------------------------------------------------
  352.     'Purpose:   Shuts down Worker and destroys objects that it has created,
  353.     '           if mlInstances = 0
  354.     '-------------------------------------------------------------------------
  355.     On Error GoTo Class_TerminateError
  356.     SetEnabled False
  357.     If gbLog Then Set goLogger = Nothing
  358.     Set goLastServiceUsed = Nothing
  359.     Set goEarlyBoundService = Nothing
  360.     If gbPersistent Then Set gcServices = Nothing
  361.     Exit Sub
  362. Class_TerminateError:
  363.     LogError Err, 0
  364.     Resume Next
  365. End Sub
  366.  
  367.